package com.sg.business.commons.editingsupport;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ViewerColumn;
import org.eclipse.swt.widgets.Composite;

import com.sg.business.model.WFHumanTaskSetting;
import com.sg.widgets.commons.editingsupport.IEditingSupportor;
import com.sg.widgets.registry.config.ColumnConfigurator;

public class EffectiveCheckEditing implements IEditingSupportor {

	@Override
	public EditingSupport createEditingSupport(final ColumnViewer viewer,
			ViewerColumn viewerColumn, final ColumnConfigurator configurator) {
		return new EditingSupport(viewer) {

			@Override
			protected void setValue(Object element, Object value) {
				if (element instanceof WFHumanTaskSetting) {
					WFHumanTaskSetting wfHumanTaskSetting = (WFHumanTaskSetting) element;
					wfHumanTaskSetting.setValue(
							WFHumanTaskSetting.F_IS_EFFECTIVE, value);
					viewer.refresh(element);
				}
			}

			@Override
			protected Object getValue(Object element) {
				if (element instanceof WFHumanTaskSetting) {
					WFHumanTaskSetting wfHumanTaskSetting = (WFHumanTaskSetting) element;
					Object value = wfHumanTaskSetting
							.isEffective();
					return value;
				}
				return null;
			}

			@Override
			protected CellEditor getCellEditor(Object element) {
				if (element instanceof WFHumanTaskSetting) {
					return new CheckboxCellEditor(
							(Composite) viewer.getControl());
				}
				return null;
			}

			@Override
			protected boolean canEdit(Object element) {
				if (element instanceof WFHumanTaskSetting) {
					return true;
				}
				return false;
			}
		};
	}
}
